<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>管理分类</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>分类信息</span></div>
                        <div>
                            <button type="button" onclick="set_chong()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">重置</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_top_row">上移</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_bottom_row">下移</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>分类名称</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height:500px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_enginid = getQueryVariable('enginid') || 0;
    var param_type = getQueryVariable('type') || 0;


    function set_chong(){
        layui.use(['laytp', 'form'], function() {
            var form = layui.form, layer = layui.layer;
            facade.ajax({type: 'get', url: ask_main_mark_guan,data:{enginid:param_enginid, type:param_type,chong:1}}).done(function(res){
                if (res.code === 2000) {

                    var list = res.data;
                    if(list.length > 0){
                        var str = '';
                        for (var key in list){
                            var nums = parseInt(key)+1;
                            str += '<tr>';
                            str += '<td style="width: 45px">'+nums+'</td>';
                            str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                            str += '</tr>';
                        }
                        $("#list_list").html(str);
                    }
                    update_refresh_row_func();

                    render_from();
                }
            });
        });
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_main_mark_guan,data:{enginid:param_enginid, type:param_type}}).done(function(res){
            if (res.code === 2000) {

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);

                var list = res.data;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }
                update_refresh_row_func();

                render_from();
            }
        });


        form.on('submit(add)', function(data){

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = $(this).find('.name').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('第'+index+'行，请输入分类名称');
                    }
                    var chongKey = check_chongfu(i, name);
                    if(chongKey){
                        throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                    }

                    list.push({
                        name:name,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.enginid = param_enginid;
            data.field.type = param_type;

            facade.ajax({url: ask_main_mark_guan, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    facade.success("提交成功!");
                    setTimeout(() => {
                        layer_close_opt();
                    }, 1500);
                }
            });
            return false;
        });

    });

    //检测重复
    function check_chongfu(index, name){
        try {
            $('#list_list tr').each(function (i) {
                var cur_name = $(this).find('.name').val();
                if(index != i){
                    if(name == cur_name){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    function short_table_opt_func(bridge_str, table_key, xmObj={}){
        if(!table_key || table_key == undefined || table_key == 'undefined'){
            table_key = 1;
        }
        update_refresh_row_func();

        $(".short_table_top_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj,sle_html;

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                    sle_html = '<tr>'+$(this).formhtml()+'</tr>';
                }
            });

            if(!sle){
                layer.msg("请选中要移动的行");return false;
            }else{
                if(sle_obj.index() > 0){
                    var prev = sle_obj.prev();
                    sle_obj.insertBefore(prev);
                }
            }
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_bottom_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj,sle_html;

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                    sle_html = '<tr>'+$(this).formhtml()+'</tr>';
                }
            });

            if(!sle){
                layer.msg("请选中要移动的行");return false;
            }else{
                var next = sle_obj.next();
                if(next){
                    sle_obj.insertAfter(next);
                }
            }
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });


        $(".short_table_add_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }

            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                $(this).parents('.short_table_list').find('table').children('tbody').append(bridge_str);
            }else{
                $(sle_obj).after(bridge_str);
            }

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('table').children('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('table').children('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_copy_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj,sle_html;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                    sle_html = '<tr>'+$(this).formhtml()+'</tr>';

                    if($(this).find('.filedetaobjdata').html() != undefined){
                        var filedetaobjdataStr = $(this).find('.filedetaobjdata').html();
                        var filedetaobjdataLength = $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html();
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(0);
                        }
                        $(this).find('.filedetaobjdata').html('[]');
                        sle_html = '<tr>'+$(this).formhtml()+'</tr>';
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(filedetaobjdataLength);
                        }
                        $(this).find('.filedetaobjdata').html(filedetaobjdataStr);
                    }
                }
            });

            if(!sle){
                layer.msg("请选中要复制的行");return false;
            }else{
                $(sle_obj).after(sle_html);
            }
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_del_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                // 样式处理
                $(this).find('td').css({'border': '1px solid #eee'});
                $('.layui-table thead th ').css({'border-bottom-color': '#eee'});

                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                layer.msg("请选中要删除的行");return false;
            }else{
                var thatss = $(this);
                layer.confirm('确定要删除该文件吗？',function(index){
                    $(sle_obj).remove();

                    $.each(thatss.parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                        $(this).children('td:first-child').html(key+1);
                    });

                    // 取消滚动条
                    if(thatss.parents('.short_table_list').find('tbody').height() < 500){
                        thatss.parents('.short_table_list').find('thead').css('overflow-y','hidden')
                    }

                    render_date();
                    render_from();
                    update_refresh_row_func();
                    set_list_render_xmselect(xmObj);
                    reload_table_list_bac('short_table_list');


                    layer.close(index);
                });
            }



            // if($(this).parents('.short_table_list').find('table').children('tbody').children('tr').length < 1){
            //     $(this).parents('.short_table_list').children('table').children('tbody').append(bridge_str);
            // }

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_del_all").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var thatss = $(this)
            layer.confirm('确定要删除该文件吗？',function(index){


                var sle = false;
                var sle_obj;
                thatss.parents('.short_table_list').find('table').children('tbody').html('');

                $.each(thatss.parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                    $(this).children('td:first-child').html(key+1);
                });

                // 取消滚动条
                if(thatss.parents('.short_table_list').find('tbody').height() < 500){
                    $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
                }

                render_date();
                render_from();
                update_refresh_row_func();
                set_list_render_xmselect(xmObj);
                reload_table_list_bac('short_table_list');
                layer.close(index);
            });

        });
    }


</script>
</body>
</html>
